package com.contory.ssn_lgnd.app;

import java.io.IOException;
import java.io.RandomAccessFile;

import com.contory.ssn_lgnd.framework.FileIO;

public class Settings {
    public static byte musicEnabled = 1;
    public static byte effectEnabled = 1;
    static final int stageNum = 6;
    static final int stageWritePos = 2;
    public static byte[] stageFlagList = new byte[stageNum];
    
    public final static String file = "setting";
    
    public static void load(FileIO files) {
    	RandomAccessFile in = null;
        try {
            in = files.randomAccessFile(file, "r");
            musicEnabled = in.readByte();
            effectEnabled = in.readByte();
            for(int i = 0; i < stageFlagList.length; i++){
            	stageFlagList[i] = in.readByte();
            }
        } catch (IOException e) {
            // :( It's ok we have defaults
        } catch (NumberFormatException e) {
            // :/ It's ok, defaults save our day
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
            }
        }
    }
    
    public static void saveStageEnabled(FileIO files, int i, boolean enabled){
    	if(enabled)
    		stageFlagList[i] = 1;
    	else
    		stageFlagList[i] = 0;
    	
    	RandomAccessFile out = null;
    	try {
        	out = files.randomAccessFile(file, "rw");
        	out.seek(stageWritePos + i);
        	out.write(stageFlagList[i]);
        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }
    
    public static void saveMusicEnabled(FileIO files, boolean enabled){
    	if(enabled)
    		musicEnabled = 1;
    	else
    		musicEnabled = 0;
    	
    	RandomAccessFile out = null;
    	try {
        	out = files.randomAccessFile(file, "rw");
        	out.write(musicEnabled);
        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }
    
    public static void saveEffectEnabled(FileIO files, boolean enabled){
    	if(enabled)
    		effectEnabled = 1;
    	else
    		effectEnabled = 0;
    	
    	RandomAccessFile out = null;
    	try {
        	out = files.randomAccessFile(file, "rw");
        	out.seek(1);
        	out.write(effectEnabled);
        } catch (IOException e) {
        } finally {
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
            }
        }
    }
}
